#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import MySQLdb
import time
import sys

time1 = time.time()

DATABASE_NAME = 'DGV_20130723'
HOST = '10.10.31.17'
PORT = '3306'
USER_NAME = 'DGV'
PASSWORD = 'DGV'
CHAR_SET = 'utf8'

INFILE = sys.argv[1]
TABLE = sys.argv[2]

conn = MySQLdb.connect(host=HOST, user=USER_NAME, passwd=PASSWORD, db=DATABASE_NAME, charset=CHAR_SET)

inputfile = open(INFILE, "r")

inputfile.readline()
for line in inputfile:
    line = line.rstrip("\n")
    if not line.startswith("#"):
        splitline = line.split("\t")
        if splitline[14] == "":
            splitline[14] = "0"
        if splitline[15] == "":
            splitline[15] = "0"
        if splitline[16] == "":
            splitline[16] = "0"
        cursor = conn.cursor()
        sql = "insert into " + TABLE + " values(0,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        params = (
            splitline[0], splitline[1], splitline[2], splitline[3], splitline[4], splitline[5], splitline[6],
            splitline[7], splitline[8], splitline[9], splitline[10], splitline[11], splitline[12], splitline[13],
            splitline[14], splitline[15], splitline[16], splitline[17], splitline[18], splitline[19])
        result = cursor.execute(sql, params)
        conn.commit()
        cursor.close()
conn.close()
inputfile.close()

time2 = time.time()
timespend = time2 - time1

print sys.argv[1] + ":" + str(timespend) + "s"




